package org.petstore.web.servlet;

import org.petstore.domain.Account;
import org.petstore.service.AccountService;
import org.petstore.service.CartService;
import org.petstore.service.CatalogService;
import org.petstore.service.impl.AccountServiceImpl;
import org.petstore.service.impl.CartServiceImpl;
import org.petstore.service.impl.CatalogServiceImpl;
import org.petstore.util.Cart;
import org.petstore.util.CartItem;
import org.petstore.web.servlet.util.MyUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;

import static org.petstore.web.servlet.util.ServletUtil.MAIN_FORM;

public class UpdateItemQuantityInCartServlet extends HttpServlet {

    private final CatalogService catalogService = MyUtil.getApplicationContext().getBean("catalogService", CatalogService.class);
    private final CartService cartService = MyUtil.getApplicationContext().getBean("cartService",CartService.class);
    private CartItem cartItem = new CartItem();
    private Cart cart = new Cart();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String itemId = request.getParameter("itemId");

        HttpSession session = request.getSession();
        Account loginUser = (Account) session.getAttribute("loginUser");
        //账号匹配 地址栏用户名与session中(Account)loginUser的username是否一致
        if (username == null || loginUser == null) {
//            request.getRequestDispatcher(MAIN_FORM).forward(request, response);
//            System.out.println("run1");
            return;
        }
        if (!username.equals(loginUser.getUsername())) {
            //非法途径
            //地址栏用户名与session中用户名不一致
            //todo 可以做一个弹窗警告 https://blog.csdn.net/NewBie_xW/article/details/108598765
            request.getRequestDispatcher(MAIN_FORM).forward(request, response);
            System.out.println("run2");
        }

        //匹配成功
        int quantity = Integer.parseInt(request.getParameter("quantity"));
        cartService.deleteInCart(loginUser.getUsername(), itemId);
        CartItem cartItem = new CartItem();
        cartItem.setItem(catalogService.getItem(itemId));
        cartItem.setQuantity(quantity);
        cartItem.setInStock(true);
        cartService.addIntoCart(loginUser.getUsername(), cartItem);
        cart = cartService.getCartByName(username);
        BigDecimal total = cart.getItemTotal(itemId);
        BigDecimal subTotal = cart.getSubTotal();
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
        //todo data用于返回给JS代码 感觉得修改一下 不清晰 用JSON??
        String data = total + "," + subTotal;
        out.print(data);//返回给JS
        out.flush();
        out.close();

        //更新session中购物车数据
        session.setAttribute("cart", cartService.getCartByName(loginUser.getUsername()));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
